import Vue from 'vue';
import App from './App.vue';
import router from './router';
import ElementUI, { Message } from 'element-ui';
import VueI18n from 'vue-i18n';
import { messages } from './components/common/i18n';
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
// import './assets/css/theme-green/index.css'; // 浅绿色主题
import './assets/css/icon.css';
import './components/common/directives';
import 'babel-polyfill';
//zcl
import axios from 'axios';
//ztree
import '../plugings/jquery-3.5.1';
import '../plugings/jquery.ztree.core';
import '../plugings/excheck';
import '../plugings/exedit';
import global from '../src/components/common/Global';
import * as echarts from "echarts";
Vue.prototype.$echarts = echarts;

Vue.prototype.global=global;

Vue.prototype.$http = axios;
//设置全局请求
axios.defaults.baseURL = 'http://127.0.0.1:8172';
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';

Vue.config.productionTip = false;
Vue.use(VueI18n);
Vue.use(ElementUI, {
    size: 'small'
});
const i18n = new VueI18n({
    locale: 'zh',
    messages
});

//请求拦截
axios.interceptors.request.use(config => {
    let token = localStorage.getItem('token');//获取本地token
    console.log(token);//本地没有，则用户未登录或清除缓存
    if (token) {
        config.headers.Authorization = token;//设置请求头携带tokenll
    }
    return config;
});


// 响应拦截,gateway返回的是html，所以用这种方式处理
axios.interceptors.response.use(function(response) {
    console.log(11);
    if (!response.data.status) {
        Message.error(response.data.msg);
    }
    return response;
}, function(error) {
    console.log(22);
    if (error.response.code == 401) {
        Message.error('登录过期，请重新登录');
        router.replace('/login');
    }else{
        Message.error('服务器丢了，难过(ಥ﹏ಥ)');
    }

});
//路由守卫，防止用户未登录访问其它路由
router.beforeEach((to, form, next) => {
    let token = localStorage.getItem('token');//获取本地token
    if (token||to.path==='/login') {
        next();
    } else {
        next('?/redirect='+'/login');
    }
});
new Vue({
    router,
    i18n,
    render: h => h(App)
}).$mount('#app');
